$(function () {
    //普通登录  快捷登录切换
    $('.selectWay li').click(function () {

        $('.selectWay li').removeClass('on');
        $(this).addClass('on');
        $('.userMessage').hide();
        setTimeout(function () {
            $('.userPhone').focus();
        }, 400);
        $('.userMessage').eq($(this).index()).slideDown(400);
    });
    //普通登录页面验证
    $(".ordinary").Validform({
        btnSubmit: "#btn_sub",
        showAllError: true,
        tiptype: 3
    });
    $(".ordinaryFast").Validform({
        btnSubmit: "#btn_fast",
        showAllError: true,
        tiptype: 3
    });
});

/**
 * 用户登录
 */
function subLogin() {
    var userPass = $('.userPass').val();
    var userName = $('.userName').val();
    var verificationCode = $('.loginCode').val();
    if (verificationCode == "") {
        return false;
    }
    if (userName == "") {
        return false;
    }
    if (userPass == "") {
        return false;
    }
    $.post('/cn/new-api/web-login', {
        verificationCode: verificationCode,
        userPass: userPass,
        userName: userName
    }, function (re) {
        if (re.code == 1) {
            _czc.push(['_trackEvent','密码登录','登录成功','',''])
            setCookie('readName', userName);
            if ($('#auto').is(':checked')) {
                setCookie('readSign', 1);
                setCookie('readPass', userPass);
            } else {
                setCookie('readSign', 0);
                delCookie('readPass');
            }
            if (re.check == 1) {//有昵称就跳
                location.href = 'https://www.thinkwithu.com?sslToken='+ re.sslToken + '&returnUrl=' + re.url;
            } else {//没有昵称先修改昵称
                $.post('/cn/new-api/change-nickname', {nickname: Date.parse(new Date())}, function (data) {
                    if (data.code == 1) {
                        location.href = 'https://www.thinkwithu.com?sslToken='+ re.sslToken + '&returnUrl=' + re.url;
                    }
                }, 'json')
            }
        } else {
            alert(re.message);
        }
    }, 'json')
}

/**
 * 短信快捷登录
 */
function fastLogin() {
    var userPhone = $('.userPhone').val();
    var source = $('#source').val();
    var verificationCode = $('.loginCodePhone').val();
    if(!$('#agreement').is(':checked')){
        alert('请勾选用户协议');
        return false;
    }
    if (verificationCode == "") {
        return false;
    }
    if (userPhone == "") {
        return false;
    }
    $.post('/cn/new-api/phone-login', {phone: userPhone, code: verificationCode, source: source}, function (re) {
        var url = re.url;
        if (re.code == 1) {
            setCookie('userPhone', userPhone);
            if ($('#account').is(':checked')) {
                setCookie('readZh', 1);
            } else {
                setCookie('readZh', 0);
                $('.userPhone').val('');
            }
            if (re.type == 1) {
                alert('首次登录你的雷哥网密码为：' + re.password);
            }
            if (re.check == 1) {
                location.href = url;
            } else {
                $.post('/cn/new-api/change-nickname', {nickname: Date.parse(new Date())}, function (data) {
                    if (data.code == 1) {
                        location.href = url;
                    }
                }, 'json')
            }
        } else {
            alert(re.message);
        }
    }, 'json')
}

/**
 * enter键登录
 */
function enterLogin(event) {
    if (event.keyCode == 13) {
        subLogin();
    }
}

function enterFastLogin(event) {
    if (event.keyCode == 13) {
        fastLogin();
    }
}

//监听
function watch() {
    if ($('.userPhone').val().length == 11) {
        $('.phoneCode').removeAttr('disabled');
        $('.phoneCode').addClass('phoneCodeVi');
    } else {
        $('.phoneCode').attr('disabled', true);
        $('.phoneCode').removeClass('phoneCodeVi');
    }
}

//发送验证码
function sendCode(obj) {
    var timeNum = 60;
    var phone = $('.userPhone').val();

    //type  1--注册  9--绑定  2--找回  其他--登录
    $.post('/cn/new-api/phone-code', {phoneNum: phone, type: 3}, function (re) {
        alert(re.message);
        if (re.code == 1) {
            $('.userPhone').attr('readonly', true);
            $(obj).attr('disabled', true);
            $(obj).removeClass('phoneCodeVi');
            $(obj).addClass('phoneCodeSi');
            var timer = setInterval(function () {
                $(obj).html(timeNum + 's获取');
                timeNum--;
                if (timeNum < 0) {
                    clearInterval(timer);
                    $('.userPhone').removeAttr('readonly');
                    $(obj).removeAttr('disabled');
                    $(obj).addClass('phoneCodeVi');
                    $(obj).removeClass('phoneCodeSi');
                    $(obj).html("获取验证码");
                }
            }, 1000);
        }

    }, 'json');
    _czc.push(['_trackEvent','快捷登录','获取验证码','',''])
}


function setCookie(name, value) {
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

//读取cookies
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

    if (arr = document.cookie.match(reg))

        return unescape(arr[2]);
    else
        return null;
}

//删除cookies
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null)
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}
